package org.example.admin.controller.lide;

import org.example.admin.entity.EleUser;
import org.example.admin.entity.Shop;
import org.example.admin.service.ShopService;
import org.example.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopService shopService;

    @GetMapping("/insert")
    public R insert(@RequestParam(value = "dishname") String dishname,
                    @RequestParam(value = "dishprice") int dishprice,
                    @RequestParam(value = "userid") int userid){

        int sign = shopService.insertShop(dishname,dishprice,userid,1);

        if (sign < 0){
            return R.fail("添加失败");
        }
        return R.ok();

    }

    @GetMapping("/show")
    public R show(@RequestBody EleUser eleUser){

        List<Shop> shop = shopService.selectShopByUserId(eleUser.getUserid());
        return R.ok(shop);
    }

    @PostMapping("/update")
    public R update(@RequestParam(value = "sign") int sign,
                    @RequestParam(value = "shopid") int shopid){
        Shop shop = shopService.selectShopByShopId(shopid);
        if(shop == null){
            return R.fail("点餐数据不存在");
        }
        int sign1 = shopService.updateNumber(sign,shopid);
        if(sign1 <= 0){
            return R.fail("修改错误");
        }
        Shop shop1 = shopService.selectShopByShopId(shopid);
        if(shop.getNumber() <= 0){
            shopService.deleteShopByShopId(shopid);
            return R.ok();
        }
        return R.ok(shop1);
    }
}
